﻿"use strict";
define(['admin/app', "service/admin/paymentService", "service/baseService"], function (app) {
    app.controller("paymentController", function ($scope, $state, $stateParams, $location, utils, paymentService,baseService, w5cValidator) {
        var methods = {
            search: function () {
                paymentService.gets().success(function (response) { $scope.model = response.data; });
            },
            edit: function () {
                baseService.banks().success(function (response) { $scope.banks = response.data; });
                if ($stateParams.id > 0) {
                    paymentService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                }
                else {
                    $scope.model = { id: 0, settles: [], bank:[] };
                    $scope.breadcrumb = '新增';
                }
                w5cValidator.setRules({
                    name: { required: '请填写名称' },
                    code: { required: '请填写编号' }
                });
            },
            goBack: function () {
                history.back();
            },
            save: function () {
                var model = $scope.model;
                model.settle_type=model.settle_type==null?"":model.settle_type;
                $scope.psdisabled = true;
                if (model.id == 0) {
                    paymentService.post(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                } else {
                    paymentService.put(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                }
            },
            remove: function (id) {
                var modal = utils.confirm({ msg: "是否需要删除当前项目", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    paymentService.delete(id).success(function () {
                        methods.search();
                    });
                })
            },
            add_settle: function () {
                $scope.model.settles.push({settle_type:''});
            },
            remove_settle: function (settle) {
                utils.remove($scope.model.settles, settle);
            }
        }
        angular.extend($scope, methods);
        if ($stateParams.action != undefined) {
            methods.edit();
        }
        else
            methods.search();
    });
});
